<?php

declare(strict_types=1);

namespace app\backend\controller\crm;

use app\common\controller\Basic;
use app\common\service\crm\SupplierService;
use app\common\service\sys\UserService;
/**
 * 供应商表 控制器
 * @author 龙
 * @date  2022-07-16
 * @copyright ©2022 泉州市盛筑信息科技有限公司 版权所有
 */
class Supplier extends Basic
{

  // 初始化
  protected function initialize()
  {
    parent::initialize();
    // 当前控制器服务实例化
    $this->service = new SupplierService();
  }

  /**
   * 列表
   */
  public function index()
  {
    if ($this->request->isGet()) {
      $data = input('param.');
      return json($this->service->_list($data));
    }
    return _jerr(1000);
  }

  /**
   * 添加
   */
  public function add()
  {
    if ($this->request->isPost()) {
      $data = input('param.');
      $this->_form_filter($data);
      return json($this->service->saveData($data));
    }
    if ($this->request->isGet()) {
      return _jok('ok', [
        'einfo' => (new UserService())->getEmployeeInfo($this->user['id']),
        'fi' => []
      ]);
    }
    return _jerr(1000);
  }

  /**
   * 编辑
   */
  public function edit()
  {
    $id = input('id/d', 0);
    if ($id === 0) {
      return _jerr(1022);
    }
    if ($this->request->isPost()) {
      $data = input('param.');
      $this->_form_filter($data);
      return json($this->service->saveData($data));
    }
    if ($this->request->isGet()) {
      $data = $this->service->getInfoById($id);
      return _jok('ok', $data);
    }
    return _jerr(1000);
  }

  /**
   * 添加编辑-表单数据处理
   */
  protected function _form_filter(&$data)
  {
    $data = rq_decode($data); // 前端已 encodeURIComponent 编码（最深二维数组） 后端需要解码
  }

  /**
   * 删除
   */
  public function del()
  {
    if ($this->request->isPost()) {
      $id = input('id', '');
      if ($id === 0) {
        return _jerr(1002);
      }
      return json($this->service->_del($id));
    }
    return _jerr(1000);
  }
  /**
   * 查看
   */
  public function look()
  {
    if ($this->request->isGet()) {
      $id = input('id/d', 0);
      $data = $this->service->getInfoById($id);
      $data['warehouseList'] = $this->service->_list($data);
      return _jok('ok', $data);
    }
    return _jerr(1000);
  }
}
